package com.mdxq.mall.sale.controller;

import cn.hutool.core.util.StrUtil;
import com.mdxq.mall.response.R;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.sale.entity.HomeBrand;
import com.mdxq.mall.sale.service.HomeBrandService;

import java.util.Arrays;
import java.util.List;

/**
 * 首页推荐品牌表 控制层。
 *
 * @author zdy
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/home/brand")
public class HomeBrandController {

    @Autowired
    private HomeBrandService homeBrandService;

    /**
     * 添加首页推荐品牌表。
     *
     * @param homeBrandList 首页推荐品牌表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("create")
    public R save(@RequestBody List<HomeBrand> homeBrandList) {
        return R.ok(homeBrandService.saveBatch(homeBrandList));
    }

    /**
     * 根据主键删除首页推荐品牌表。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("delete")
    public R remove(@RequestParam String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return R.ok(homeBrandService.removeByIds(idList));
    }

    /**
     * 根据主键更新首页推荐品牌表。
     *
     * @param homeBrand 首页推荐品牌表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody HomeBrand homeBrand) {
        return homeBrandService.updateById(homeBrand);
    }

    /**
     * 根据主键更新排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("update/sort")
    public R updateSort(@RequestParam Long id,@RequestParam Integer sort) {
        HomeBrand homeBrand = UpdateEntity.of(HomeBrand.class,id);
        homeBrand.setSort(sort);
        return R.ok(homeBrandService.updateById(homeBrand,true));
    }

    @PostMapping("update/recommendStatus")
    public R updateStatus(@RequestParam String ids,@RequestParam Integer recommendStatus) {
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id : idList) {
            HomeBrand homeBrand = UpdateEntity.of(HomeBrand.class,id);
            homeBrand.setRecommendStatus(recommendStatus);
            homeBrandService.updateById(homeBrand,true);
        }

        return R.ok(true);
    }
    /**
     * 查询所有首页推荐品牌表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<HomeBrand> list() {
        return homeBrandService.list();
    }

    /**
     * 根据首页推荐品牌表主键获取详细信息。
     *
     * @param id 首页推荐品牌表主键
     * @return 首页推荐品牌表详情
     */
    @GetMapping("getInfo/{id}")
    public HomeBrand getInfo(@PathVariable Long id) {
        return homeBrandService.getById(id);
    }

    /**
     * 分页查询首页推荐品牌表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public R page(Page<HomeBrand> page,String brandName,Integer recommendStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(brandName)){
            queryWrapper.like("brand_name",brandName);
        }
        if (recommendStatus!=null){
            queryWrapper.eq("recommend_status",recommendStatus);
        }
        return R.ok(homeBrandService.page(page,queryWrapper));
    }

}
